package com.hugedata.cdn.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * 
 * 项目名称：cdn 类名称：ZipTool 类描述： 创建人：zhaoaihua 创建时间�?013-3-23 下午7:32:05 修改备注�?
 * 
 * @version
 * 
 */
public final class ZipTool extends FileTool
{

    private static ZipTool tool;

    private ZipTool()
    {
    }

    public static ZipTool getinstance()
    {

        if (null == tool)
        {
            tool = new ZipTool();
        }

        return tool;
    }

    /**
     * 将指定文件或文件夹压缩成指定的目标ZIP文件<只支持UTF-8的文件名�?
     * 
     * @param srcFile
     *            源文�?
     * @param destZipFile
     *            目标ZIP文件
     * @param contain
     *            是否包含自身文件夹，当srcFile为文件夹是有�?
     * @throws IOException
     *             文件操作错误
     */
    public void zip(File srcFile, File destZipFile, boolean contain)
            throws IOException
    {
        if (!srcFile.exists())
        {
            throw new FileNotFoundException(srcFile.toString());
        }

        ZipOutputStream out = null;
        try
        {
            out = new ZipOutputStream(new FileOutputStream(destZipFile));
            if (srcFile.isFile() || contain)
            {
                zipSingle(out, srcFile, "");
            }
            else
            {
                for (File file : srcFile.listFiles())
                {
                    zipSingle(out, file, "");
                }
            }
            out.flush();
        }
        finally
        {
            closeStream(out);
        }
    }

    /**
     * 解压�?
     * 
     * @param srcZipFile
     *            源ZIP文件
     * @param destFilePath
     *            �?��解压的目标目录文�?
     * @throws IOException
     *             文件访问异常
     */
    public void unZip(File srcZipFile, File destFilePath) throws IOException
    {
        if (!srcZipFile.exists())
        {
            // zip 文件必须存在
            throw new FileNotFoundException(srcZipFile.toString());
        }

        if (!srcZipFile.isFile())
        {
            // zip 文件不能为目录文�?
            throw new FileNotFoundException("srcZipFile:"
                    + srcZipFile.toString() + " must be a zip file.");
        }

        if (destFilePath.isFile())
        {
            // 目标文件必须为目�?
            throw new IOException("destFilePath:" + destFilePath.toString()
                    + " must be a directory file.");
        }

        ZipInputStream zipIn = new ZipInputStream(new FileInputStream(
                srcZipFile));

        try
        {
            ZipEntry entry;
            while ((entry = zipIn.getNextEntry()) != null)
            {
                if (entry.isDirectory())
                {
                    createDirectory(destFilePath, entry.getName());
                }
                else
                {
                    createFile(destFilePath, entry.getName(), zipIn);
                }
            }
        }
        finally
        {
            closeStream(zipIn);
        }
    }

    private void zipSingle(ZipOutputStream out, File srcFile, String parentPath)
            throws IOException
    {
        if (srcFile.isFile())
        {
            // 压缩单个文件
            ZipEntry zipEntry = new ZipEntry(parentPath + srcFile.getName());
            out.putNextEntry(zipEntry);
            InputStream in = new FileInputStream(srcFile);
            try
            {
                write(in, out);
            }
            finally
            {
                closeStream(in);
            }
        }
        else
        {
            // 压缩单个目录
            ZipEntry zipEntry = new ZipEntry(parentPath + srcFile.getName()
                    + "/");
            out.putNextEntry(zipEntry);
            for (File file : srcFile.listFiles())
            {
                // 遍历压缩子文�?
                zipSingle(out, file, zipEntry.getName());
            }
        }
    }

    private void createFile(File destFilePath, String child, InputStream in)
            throws IOException
    {
        OutputStream out = new FileOutputStream(new File(destFilePath, child),
                false);
        try
        {
            write(in, out);
        }
        finally
        {
            closeStream(out);
        }
    }

    public static void main(String[] args) throws IOException
    {

        FileTool tool = new ZipTool();

        String oldpath = "D:/billtest/MonitoredData/send";

        String now = CommonUtil.getNowTimestamp("yyyyMMddHHmmss");

        String newpath = oldpath + System.getProperty("file.separator") + now;

        tool.createDirectory(new File(oldpath), now);

        String[] tests = tool.getFileList(oldpath);

        for (String aa : tests)
        {

            tool.moveFile(oldpath + System.getProperty("file.separator") + aa,
                    newpath + System.getProperty("file.separator") + aa);

            System.out.println("----");

        }

    }
}
